﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//游戏开始时部分方法执行顺序：
//①Initializer和SerciviceLocator的Awake
//②EventSystem的Awake
//③所有Service的Awake
//④所有脚本的Start和所有scripts的Initialize

/// <summary>
/// 游戏初始化器，游戏启动后自毁
/// </summary>
[DefaultExecutionOrder(-200)]
public class Initializer : Singleton<Initializer>
{
    [SerializeField]
    private int count_initializations;
    /// <summary>
    /// 剩余的初始化任务数
    /// </summary>
    public int Count_Initializations
    {
        get => count_initializations;
        set
        {
            if (value < 0 || value == count_initializations)
                return;
            if (value == 0)
                StartGame();
            count_initializations = value;
        }
    }

    protected override void Awake()
    {
        base.Awake();
        count_initializations = 0;
        Random.InitState(System.DateTime.Now.Second);
        StartCoroutine(AutoStart());
    }

    private void StartGame()
    {
        StopAllCoroutines();
        SceneController sceneController = Service.Get<SceneController>();
        Debug.Log("初始化完成");
        sceneController.LoadLevel(SceneController.Index_Boot);
        Destroy(gameObject);
    }

    private IEnumerator AutoStart()
    {
        yield return new WaitForSeconds(1f);
        if (Count_Initializations == 0)
            StartGame();
    }
}